package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.*;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.ArticleApi;
import com.jmp.feign.ArticleFeign;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.ArticleApiAdminPcMapper;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.Article;
import one.stand.model.ArticleWithBLOBs;
import one.stand.model.CompanyCustomerLable;
import one.stand.model.ResultModel;
import one.stand.service.ArticleService;
import one.stand.service.sys.UserContentService;
import one.stand.util.DateUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 */
@Api(description = "文章管理")
@RestController
@RequestMapping("adminApi/article")
@Slf4j
public class ArticleController {

    @Autowired
    private ArticleApiAdminPcMapper articleApiAdminPcMapper;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleFeign articleFeign;

    @ApiOperation(value = "列表")
    @PostMapping("")
    @SecurityAuthCheck
    public ResultModel<ArticleListResponse> list(@RequestBody ArticleListRequest request) {

        String nameLike = SqlUtil.like(request.getNameLike());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<Article> articles = articleApiAdminPcMapper.selectArticle(nameLike, request.getLabelId(), UserContentService.TYPE_ARTICLE, ArticleApi.DEFAULT_COMPANYID);
        PageResponse2<ArticleListResponse.Article> articlePage = ConvertUtils.page2(articles, article -> ArticleListResponse.Article.builder()
                .id(article.getId())
                .title(article.getTitle())
                .shareCard(article.getShareCard())
                .status(article.getStatus())
                .auditTime(DateUtil.format(DateUtil.formatter, article.getUpTime()))
                .position(article.getPosition())
                .forwardCount(article.getForwardCount())
                .viewCount(article.getViewCount())
                .build());

        PageHelper.startPage(1, 100);// 100 标签应该不会超过100
        List<CompanyCustomerLable> labels = companyApiAdminMapper.selectLabel(ArticleApi.DEFAULT_COMPANYID, null);
        List<ArticleListResponse.Label> labelList = ConvertUtils.list(labels, label -> ArticleListResponse.Label.builder()
                .id(label.getLableId())
                .name(label.getLableName())
                .build());

        ArticleListResponse response = ArticleListResponse.builder()
                .articles(articlePage)
                .labelList(labelList)
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "保存")
    @PostMapping("save")
    @SecurityAuthCheck
    public ResultModel save(@RequestBody ArticleSaveRequest request) {
        com.jmp.base.dto.ArticleSaveRequest request1 = com.jmp.base.dto.ArticleSaveRequest.builder()
                .id(request.getId())
                .templateType(request.getTemplateType())
                .adContent(request.getAdContent())
                .companyId(ArticleApi.DEFAULT_COMPANYID)
                .content(request.getContent())
                .originName(request.getOriginName())
                .originUrl(request.getOriginUrl())
                .shareCard(request.getShareCard())
                .subtitle(request.getSubtitle())
                .title(request.getTitle())
                .typeId(request.getTypeId())
                .build();
        return articleFeign.articleSave(request1);
    }

    @ApiOperation(value = "详情")
    @PostMapping("detail")
    @SecurityAuthCheck
    public ResultModel<ArticleDetailResponse> detail(@RequestBody ArticleDetailRequest request) {

        ArticleWithBLOBs article = articleService.get(request.getId());

        ArticleDetailResponse response = ArticleDetailResponse.builder()
                .id(article.getId())
                .templateType(article.getTemplateType())
                .title(article.getTitle())
                .subtitle(article.getSubtitle())
                .shareCard(article.getShareCard())
                .content(article.getContent())
                .adContent(article.getAdContent())
                .typeId(article.getTypeId())
                .originName(article.getOriginName())
                .originUrl(article.getOriginUrl())
                .build();
        return ResultModel.success(response);
    }

    @ApiOperation(value = "更新状态：发布/下架")
    @PostMapping("status/save")
    @SecurityAuthCheck
    public ResultModel statusSave(@RequestBody ArticleStatusSaveRequest request) {
        com.jmp.base.dto.ArticleStatusSaveRequest request1 = com.jmp.base.dto.ArticleStatusSaveRequest.builder()
                .id(request.getId())
                .isPush(request.getIsPush())
                .labels(request.getLabels())
                .position(request.getPosition())
                .status(request.getStatus())
                .companyId(ArticleApi.DEFAULT_COMPANYID)
                .build();
        return articleFeign.articleStatusSave(request1);
    }

    @ApiOperation(value = "更新位置：首页/置顶/轮播")
    @PostMapping("position/save")
    @SecurityAuthCheck
    public ResultModel positionSave(@RequestBody ArticlePositionSaveRequest request) {
        com.jmp.base.dto.ArticlePositionSaveRequest request1 = com.jmp.base.dto.ArticlePositionSaveRequest.builder()
                .id(request.getId())
                .position(request.getPosition())
                .build();
        return articleFeign.articlePositionSave(request1);
    }
}
